package com.etop.jansing.entities;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

import javax.persistence.*;

/**
 * 权限键值由专门的属性控制
 * Created by jansing on 2015/10/25.
 */
@Entity
@Table(name = "resourcepermission")
public class ResourcePermission extends BaseEntity {

    @ManyToOne(targetEntity = Resource.class, fetch = FetchType.EAGER)
    @Fetch(FetchMode.JOIN)
    @JoinColumn(name = "rid")
    private Resource resource;
    @ManyToOne(targetEntity = Permission.class, fetch = FetchType.EAGER)
    @Fetch(FetchMode.JOIN)
    @JoinColumn(name = "pid")
    private Permission permission;
    /**
     * 资源权限对字符串，用于shiro判断及权限页面显示
     */
    @Column(name="resoPermString")
    private String resoPermString;
    /**
     * 资源权限对中文名，用于权限页面显示
     */
    @Column
    private String cnName;

    public ResourcePermission() {
    }

    public ResourcePermission(Resource resource, Permission permission) {
        this.resource = resource;
        this.permission = permission;
        this.resoPermString = resource.getName()+":"+permission.getName();
    }

    public Resource getResource() {
        return resource;
    }

    /**
     * 资源的修改不改变权限键值
     * @param resource
     */
    public void setResource(Resource resource) {
        this.resource = resource;
//        if(permission!=null){
//            this.resoPermString = resource.getName()+":"+permission.getName();
//        }
    }

    public Permission getPermission() {
        return permission;
    }

    /**
     * 权限的修改不改变权限键值
     * @param permission
     */
    public void setPermission(Permission permission) {
        this.permission = permission;
//        if(resource!=null){
//            this.resoPermString = resource.getName()+":"+permission.getName();
//        }
    }

    public String getResoPermString() {
        return resoPermString;
    }

    public void setResoPermString(String resoPermString) {
        this.resoPermString = resoPermString;
    }

    public String getCnName() {
        return cnName;
    }

    public void setCnName(String cnName) {
        this.cnName = cnName;
    }
}
